package com.cems.service.impl;

import com.cems.mapper.ForumMapper;
import com.cems.pojo.*;
import com.cems.service.ForumService;
import com.cems.util.RedisUtil;
import com.cems.util.SelectRedis;
import org.apache.velocity.runtime.directive.contrib.For;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @ClassName ForumServiceImpl
 * @Author 陈新予(blank)
 * @Date 2021/5/18
 * @Version 1.0
 */
@Service
public class ForumServiceImpl implements ForumService {
    @Autowired
    ForumMapper forumMapper;
    @Autowired
    RedisUtil redisUtil;

    @Override
    public int delArticeById(int id) {
        return forumMapper.delArticeById(id);
    }

    @Override
    public List<ForumArticle> getArticle() {
        try {
            List<Object> objects = SelectRedis.selectRedis(redisUtil, "cems_allArticle", forumMapper, "getAllArticle");
            List<ForumArticle> list = new ArrayList<>();
            for (Object object : objects) {
                if (object instanceof ForumArticle) {
                    list.add((ForumArticle) object);
                }
            }
            return list;
        } catch (NoSuchMethodException | IllegalAccessException | InstantiationException | InvocationTargetException e) {
            e.printStackTrace();
            return forumMapper.getAllArticle();
        }
    }

    @Override
    public List<ForumComment> getComment() {
        try {

            List<Object> objects = SelectRedis.selectRedis(redisUtil, "cems_allComment", forumMapper, "getAllComment");
            List<ForumComment> list = new ArrayList<>();
            for (Object object : objects) {
                if (object instanceof ForumComment) {
                    list.add((ForumComment) object);
                }
            }
            return list;
        } catch (NoSuchMethodException | IllegalAccessException | InstantiationException | InvocationTargetException e) {
            e.printStackTrace();
            return forumMapper.getAllComment();
        }
    }

    @Override
    public List<CommentReply> getReply() {
        try {
            List<Object> objects = SelectRedis.selectRedis(redisUtil, "cems_allReply", forumMapper, "getAllReply");
            List<CommentReply> list = new ArrayList<>();
            for (Object object : objects) {
                if (object instanceof CommentReply)
                    list.add((CommentReply) object);
            }
            return list;
        } catch (NoSuchMethodException | IllegalAccessException | InstantiationException | InvocationTargetException e) {
            e.printStackTrace();
            return forumMapper.getAllReply();
        }
    }

    @Override
    public List<CommentReply> getReplyByComment(int id) {
        try {
            List<Object> objects = SelectRedis.selectRedis(redisUtil, "cems_Reply_"+id, forumMapper, "getReplyByComment", Integer.valueOf(id), 60 * 30);
            List<CommentReply> list = new ArrayList<>();
            for (Object object : objects) {
                if (object instanceof CommentReply) {
                    list.add((CommentReply) object);
                }
            }
            return list;
        } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
            e.printStackTrace();
            return forumMapper.getReplyByComment(id);
        }
    }

    @Override
    public void updateArticle(int id, String status) {
        forumMapper.updateArticle(id, status);
        redisUtil.delete("cems_allArticle");
    }

    @Override
    public void delReply(int id) {
        forumMapper.delReply(id);
        redisUtil.delete("cems_Reply_" + id);
    }

    /**
     * 查询已经审核的帖子
     *
     * @return
     */
    @Override
    public List<ForumArticle> getAllArticle2() {
        return forumMapper.getAllArticle2();
    }

    @Override
    public List<ForumArticle> selArticleByUId(int userId) {
            return forumMapper.selArticleByUId(userId);
    }

    @Override
    public ForumArticle selArticleById(int id) {
        return forumMapper.selArticleById(id);
    }

    @Override
    public int delCommentById(Map<String,Object> map) {
        redisUtil.delete("cems_allArticle");
        redisUtil.delete("cems_allComment");
        return forumMapper.delCommentById(map);
    }

    @Override
    public List<ForumComment> selForCommentById(int artId) {
        return forumMapper.selForCommentById(artId);
    }

    @Override
    public List<ForumArticle> selDelArticleByUId(int artId) {
        return forumMapper.selDelArticleByUId(artId);
    }


    /**
     * 添加帖子
     *
     * @param map
     */
    @Override
    public void addForum(Map<String, Object> map) {
        redisUtil.delete("cems_allArticle");
        forumMapper.addForum(map);
    }

    @Override
    public ForumArticle getArticleById(int id) {
        return forumMapper.getArticleById(id);
    }

    @Override
    public List<ForumCommentBig> getForumCommentListByArt(int id) {
        return forumMapper.getCommentByArt(id);
    }

    @Override
    public int addComment(Map<String, String> map) {
        forumMapper.addComment(map);
        redisUtil.delete("cems_allComment");
        return Integer.parseInt(map.get("commentId"));
    }

    @Override
    public void addReply(Map<String, String> map, int id) {
        forumMapper.addReply(map);
        redisUtil.delete("cems_Reply_" + id);
    }
}
